package com.yuncheng.spcyApi.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.spcyApi.mapper.SysUserFgldMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.IYcSysOrgService;
import com.yuncheng.spcyApi.service.IYcSysUserService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.spcyApi.entity.SysUserFgld;
import com.yuncheng.spcyApi.service.ISysUserFgldService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.vo.common.SysUserFgldVo;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import utils.LogUtils;
import utils.ObjectToMapUtils;

/**
 * @Description: 系统-分管领导配置
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Api(tags = "系统-分管领导配置接口")
@RestController
@RequestMapping("/api/spzx-spcy/sysUserFgld")
public class SysUserFgldController extends BaseController<SysUserFgld, ISysUserFgldService> {

    private static final Logger log = LoggerFactory.getLogger(SysUserFgldController.class);

    @Resource
    private ISysUserFgldService sysUserFgldService;

    @Resource
    private SysUserFgldMapper sysUserFgldMapper;

    @Resource
    private IYcSysUserService ycSysUserService;

    @Resource
    private IYcSysOrgService ycSysOrgService;

    @Resource
    private ISpcyLogService spcyLogService;


    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Log(title = "系统-分管领导配置-分页列表查询")
    @ApiOperation(value = "系统-分管领导配置-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(SysUserFgldVo sysUserFgldVo, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysUserFgldVo> page = new Page<>(pageNo, pageSize);
        IPage<SysUserFgldVo> pageList = sysUserFgldMapper.getSysFgldListPage(page, sysUserFgldVo);
        return HttpResult.ok(pageList);
    }


    /**
     * 获取所以用户和部门
     * @return
     */
    @Log(title = "获取所以用户和部门")
    @ApiOperation(value = "获取所以用户和部门")
    @GetMapping(value = "/getAllUserAndDeptList")
    public HttpResult<?> getAllUserAndDeptList(){
        Map map = new HashMap();
        List<YcSysOrg> deptList = ycSysOrgService.list(new LambdaQueryWrapper<YcSysOrg>()
                .eq(YcSysOrg::getSubOrgNum, 0));

        List<YcSysUser> userList = ycSysUserService.list();
        map.put("userList",userList);
        map.put("deptList",deptList);
        return HttpResult.ok(map);
    }

    /**
     * 添加
     *
     * @param sysUserFgld
     * @return
     */
    @Log(title = "系统-分管领导配置-添加")
    @ApiOperation(value = "系统-分管领导配置-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody SysUserFgld sysUserFgld) {
        sysUserFgldService.save(sysUserFgld);
        String operContent = "新增信息：";

        // 将map里面的属性和值转String
        Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(sysUserFgld);
        operContent += LogUtils.MapToStringMethod(beanMap);
        spcyLogService.addLogs(LogConstant.LOGS_SYS_FGLD,operContent,sysUserFgld.getId(), LogConstant.LOGS_SYS_FGLD, SpcyConstant.LOGS_SYS_TYPE_XZ);
        return HttpResult.ok("添加成功");
    }

    /**
     * 编辑
     *
     * @return
     */
    @Log(title = "系统-分管领导配置-编辑")
    @ApiOperation(value = "系统-分管领导配置-编辑")
    @ApiImplicitParam(name = "id",value = "分管领导配置id",required = true)
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("分管领导配置id不能为空");
        }
        String gcyId = buffer.get("id");
        SysUserFgld oldFgld = sysUserFgldService.getById(gcyId);
        SysUserFgld fgldUpdate = sysUserFgldService.getById(gcyId);
        if (fgldUpdate == null){
            return HttpResult.error("当前分管领导信息数据不存在，请重试");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(fgldUpdate, buffer);
        sysUserFgldService.updateById(fgldUpdate);

        String operContent = "基本信息修改：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldFgld);
        operContent += LogUtils.BeanModifyMsg(fgldUpdate,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_SYS_FGLD,operContent,gcyId,LogConstant.LOGS_SYS_FGLD,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @Log(title = "系统-分管领导配置-通过id删除")
    @ApiOperation(value = "系统-分管领导配置-通过id删除")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        if (StringUtils.isEmpty(id)){
            return HttpResult.error("id参数不能为空");
        }
        SysUserFgld nowFgld = sysUserFgldService.getById(id);
        if (nowFgld == null){
            return HttpResult.error("当前分管领导数据不存在，请重试");
        }
        sysUserFgldService.removeById(id);
        String operContent = "删除信息：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(nowFgld);
        operContent += LogUtils.MapToStringMethod(oldMap);
        spcyLogService.addLogs(LogConstant.LOGS_SYS_FGLD,operContent,id,LogConstant.LOGS_SYS_FGLD,SpcyConstant.LOGS_SYS_TYPE_SC);
        return HttpResult.ok("删除成功");
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return
     */
    @Log(title = "系统-分管领导配置-批量删除")
    @ApiOperation(value = "系统-分管领导配置-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(","));
        List<SysUserFgld> sysUserFgldList= sysUserFgldService.listByIds(idsList);
        if (CollectionUtils.isEmpty(sysUserFgldList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SysUserFgld fgld : sysUserFgldList){
            String id = fgld.getId();
            sysUserFgldService.removeById(id);

            operContent = "删除信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(fgld);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_SYS_FGLD,operContent,id,LogConstant.LOGS_SYS_FGLD,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";
        }

        return HttpResult.ok("删除成功");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Log(title = "系统-分管领导配置-通过id查询")
    @ApiOperation(value = "系统-分管领导配置-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        if (StringUtils.isEmpty(id)){
            return HttpResult.error("分管领导id不能为空");
        }
        SysUserFgldVo sysUserFgldVo = new SysUserFgldVo();
        sysUserFgldVo.setId(id);
        List<SysUserFgldVo> sysFgldList = sysUserFgldMapper.getSysFgldList(sysUserFgldVo);
        if (CollectionUtils.isNotEmpty(sysFgldList)){
            return HttpResult.ok(sysFgldList.get(0));
        }
        return HttpResult.ok(null);
    }

    /**
     * 数据转换
     * @param sysUserFgld
     */
    private void formatValue(SysUserFgld sysUserFgld){
    }
}
